package xhwl.hmo.mall.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on 2017-11-01.
 */

public class IdcardValidator {
    private static String[] cityCode = new String[]{"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82", "91"};
    private static int[] power = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    public IdcardValidator() {
    }

    public static boolean isValidatedAllIdcard(String idcard) {
        return idcard != null && !"".equals(idcard)?(idcard.length() == 15?validate15IDCard(idcard):StringUtil.isEmpty(validate18Idcard(idcard))):false;
    }

    public static String validate18Idcard(String idcard) {
        String msg = "";
        if(idcard == null) {
            msg = "身份证号不能为空！";
            return msg;
        } else if(idcard.length() != 18) {
            msg = "身份证号必须为18位！";
            return msg;
        } else {
            String idcard17 = idcard.substring(0, 17);
            if(!isDigital(idcard17)) {
                msg = "身份证号前17位必须为数字！";
                return msg;
            } else {
                String provinceid = idcard.substring(0, 2);
                if(!checkProvinceid(provinceid)) {
                    msg = "身份证号省份校验错误！";
                    return msg;
                } else {
                    String birthday = idcard.substring(6, 14);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

                    try {
                        Date idcard18Code = sdf.parse(birthday);
                        String c = sdf.format(idcard18Code);
                        if(!c.equals(birthday)) {
                            msg = "身份证号出生日期错误！";
                            return msg;
                        }
                    } catch (ParseException var11) {
                        msg = "身份证号出生日期不合法！";
                        return msg;
                    }

                    String idcard18Code1 = idcard.substring(17, 18);
                    char[] c1 = idcard17.toCharArray();
                    int[] bit = converCharToInt(c1);
                    boolean sum17 = false;
                    int sum171 = getPowerSum(bit);
                    String checkCode = getCheckCodeBySum(sum171);
                    if(null == checkCode) {
                        msg = "身份证号校验不合法！";
                        return msg;
                    } else if(!idcard18Code1.equalsIgnoreCase(checkCode)) {
                        msg = "身份证号校验不合法！";
                        return msg;
                    } else {
                        return msg;
                    }
                }
            }
        }
    }

    public static boolean validate15IDCard(String idcard) {
        if(idcard == null) {
            return false;
        } else if(idcard.length() != 15) {
            return false;
        } else if(!isDigital(idcard)) {
            return false;
        } else {
            String provinceid = idcard.substring(0, 2);
            if(!checkProvinceid(provinceid)) {
                return false;
            } else {
                String birthday = idcard.substring(6, 12);
                SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");

                try {
                    Date e1 = sdf.parse(birthday);
                    String tmpDate = sdf.format(e1);
                    return tmpDate.equals(birthday);
                } catch (ParseException var6) {
                    return false;
                }
            }
        }
    }

    public static String convertIdcarBy15bit(String idcard) {
        if(idcard == null) {
            return null;
        } else if(idcard.length() != 15) {
            return null;
        } else if(!isDigital(idcard)) {
            return null;
        } else {
            String provinceid = idcard.substring(0, 2);
            if(!checkProvinceid(provinceid)) {
                return null;
            } else {
                String birthday = idcard.substring(6, 12);
                SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
                Date birthdate = null;

                try {
                    birthdate = sdf.parse(birthday);
                    String cday = sdf.format(birthdate);
                    if(!cday.equals(birthday)) {
                        return null;
                    }
                } catch (ParseException var12) {
                    return null;
                }

                Calendar cday1 = Calendar.getInstance();
                cday1.setTime(birthdate);
                String year = String.valueOf(cday1.get(1));
                String idcard17 = idcard.substring(0, 6) + year + idcard.substring(8);
                char[] c = idcard17.toCharArray();
                String checkCode = "";
                int[] bit = converCharToInt(c);
                boolean sum17 = false;
                int sum171 = getPowerSum(bit);
                checkCode = getCheckCodeBySum(sum171);
                if(null == checkCode) {
                    return null;
                } else {
                    idcard17 = idcard17 + checkCode;
                    return idcard17;
                }
            }
        }
    }

    private static boolean checkProvinceid(String provinceid) {
        String[] var1 = cityCode;
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            String id = var1[var3];
            if(id.equals(provinceid)) {
                return true;
            }
        }

        return false;
    }

    private static boolean isDigital(String str) {
        return str.matches("^[0-9]*$");
    }

    private static int getPowerSum(int[] bit) {
        int sum = 0;
        if(power.length != bit.length) {
            return sum;
        } else {
            for(int i = 0; i < bit.length; ++i) {
                for(int j = 0; j < power.length; ++j) {
                    if(i == j) {
                        sum += bit[i] * power[j];
                    }
                }
            }

            return sum;
        }
    }

    private static String getCheckCodeBySum(int sum17) {
        String checkCode = null;
        switch(sum17 % 11) {
            case 0:
                checkCode = "1";
                break;
            case 1:
                checkCode = "0";
                break;
            case 2:
                checkCode = "x";
                break;
            case 3:
                checkCode = "9";
                break;
            case 4:
                checkCode = "8";
                break;
            case 5:
                checkCode = "7";
                break;
            case 6:
                checkCode = "6";
                break;
            case 7:
                checkCode = "5";
                break;
            case 8:
                checkCode = "4";
                break;
            case 9:
                checkCode = "3";
                break;
            case 10:
                checkCode = "2";
        }

        return checkCode;
    }

    private static int[] converCharToInt(char[] c) throws NumberFormatException {
        int[] a = new int[c.length];
        int k = 0;
        char[] var3 = c;
        int var4 = c.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            char temp = var3[var5];
            a[k++] = Integer.parseInt(String.valueOf(temp));
        }

        return a;
    }

    public static void main(String[] args) throws Exception {
        String idcard15 = "130321860311519";
        String idcard18 = "210102198617083732";
        System.out.println(isValidatedAllIdcard(idcard15));
        System.out.println(isValidatedAllIdcard(idcard18));
        System.out.println(convertIdcarBy15bit(idcard15));
    }
}
